<?php
/**
 * @package		Joomla.Administrator
 * @subpackage	com_users
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;



// Load the tooltip behavior.
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');


$params = JComponentHelper::getParams('com_brinkmanvm');
$pathCat = $params->get('pathcategories','');
$pathProducts = $params->get('pathproducts','');

$logs = JRequest::getVar('logs',0);
if($logs):
    ?>
    <style>
        table.tableLogs td{
            padding: 10px;
        }
        table.tableLogs th {
            font-weight: bold;
            background: #f9f9f9;
        }
    </style>
    <h2>Successful Logs</h2>

<?php
// Get a db connection.
    $db = JFactory::getDbo();

// Create a new query object.
    $query = $db->getQuery(true);

// Select all records from the user profile table where key begins with "custom.".
// Order it by the ordering field.
    $query->select('*');
    $query->from($db->quoteName('bkvm_logs'));
    $query->order('date ASC');

// Reset the query using our newly populated query object.
    $db->setQuery($query);

// Load the results as a list of stdClass objects (see later for more options on retrieving data).
    $results = $db->loadObjectList();

    ?>
    <table class="table tableLogs">

        <tr>
            <th>#</th>
            <th>Date</th>
            <th>products_path</th>
            <th>categories_path</th>

        </tr>
        <?php foreach ($results as $r):?>

            <tr>
                <td><?php echo $r->id;?></td>
                <td><?php echo $r->date;?></td>
                <td><?php echo $r->products_path;?></td>
                <td><?php echo $r->categories_path;?></td>
            </tr>

        <?php endforeach;?>
    </table>
    <br>
    <a href="index.php?option=com_brinkmanvm&logs=0">View Import</a>

<?php else:?>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

    <script type="text/javascript">
        Joomla.submitbutton = function(task)
        {
            if (task == 'user.cancel' || document.formvalidator.isValid(document.id('user-form'))) {
                Joomla.submitform(task, document.getElementById('user-form'));
            }
        }


        jQuery(document).ready(function($) {
            $(document).on("click", "#process", function(){

                if ($("#notfound").length > 0){
                    alert("Check the path of the csv files");
                }else{

                    $.ajax({
                        url: "<?php echo JURI::root();?>index.php?option=com_brinkmanvm&task=process&format=raw",
                        type: "GET",
                        timeout: 3600000, //Set your timeout value, 3600000 = 60 minutes
                        beforeSend: function (  ) {
                            $('div#loadingVM').fadeIn();
                        },
                        success: function(response) {

                            $('div#loadingVM').fadeOut();
                            alert("Products/categories imported!");
                        },
                        error: function(jqXHR, textStatus, errorThrown) {
                            $('div#loadingVM').fadeOut();
                            if(textStatus==="timeout") {
                                alert("Call has timed out"); //Handle the timeout
                            } else {
                                alert(errorThrown); //Handle other error type
                            }
                        }
                    });
                }
            });

        });


    </script>
    <div id="loadingVM" style="background-color:#fff; padding:10px; display:none;">
        <img src="<?php echo JURI::root();?>/administrator/components/com_brinkmanvm/assets/images/ajax-loader.gif"> Importing products and categories, it will take up to 45 minutes...
    </div>

    <h2>Brinkman Vrituemart Importer</h2>

    <br />

    <?php
    if(!file_exists($pathProducts)) {
        echo '<div id="notfound"></div><p style="color:red;">File "'.$pathProducts.'" not found. Make sure you specified the correct path and that you upload the correct file.</span><br>';
    }
    else{
        echo '<p style="color:green;">File "'.$pathProducts.'" is correct.</span><br>';
    }
    if(!file_exists($pathCat)) {
        echo '<div id="notfound"></div><p style="color:red;">File "'.$pathCat.'" not found. Make sure you specified the correct path and that you upload the correct file.</span><br>';
    }
    else{
        echo '<p style="color:green;">File "'.$pathCat.'" is correct.</span><br>';
    }

    ?>
    <br><br>
    <a style="color:white; background:#222; padding:4px;" href="<?php echo JURI::root();?>index.php?option=com_brinkmanvm&task=process&format=raw" target="_blank">Process products groups and images</a>
    <!-- <button id="process">Process</button>-->
    <br /><br />
    <a style="color:white; background:#222; padding:4px;" href="<?php echo JURI::root();?>index.php?option=com_brinkmanvm&task=importImages&format=raw" target="_blank">Import only images</a>
    <br /><br />

    <a href="index.php?option=com_brinkmanvm&logs=1">View Logs</a>
<?php endif;?>
